<template>
    <div class="songlistbox">
        <!-- 专辑页 -->
        <div v-show="songlistxs">
            <!-- 返回按键， 顶部歌单名 -->
            <mytop v-bind:thisname="allname" v-bind:gdm="alb"></mytop>

            <!-- 歌单详情 -->
            <div class="listtoubox">
                <div class="listbgimg">
                    <div class="zhezhao" ></div>
                    <div class="zzd" :style='{background:`rgb(${bacolor.toString()})`}' ></div>
                </div>
                <div class="listsmallbox">
                    <div class="listleft">
                        <div class="ltitle">
                            <img :src="imgs.bgimg" id="myimg" @load="loadImage" alt="">
                        </div>
                    </div>
                    <div class="listright">
                        <div class="listtitle">
                            <div class="listtouimg">
                                <div class="zjname">新歌速递</div>
                                <div class="zjpeople">{{imgs.thepeople}} </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <ul class="listcontentbox" ref="ul">
                <li v-for="(item,index) in songarr" :key="item.id">
                    <listone v-bind:myindex="index" v-bind:myid="item.id" v-bind:mypeople="item.people" v-bind:myname="item.name"></listone>
                </li>
                <li class="loading" v-show="!songlistend">
                    <loading></loading>
                </li>
            </ul>
 
        </div>
        <!-- /comment/album?id=32311专辑评论 -->
        <loading v-show="!songlistxs"></loading>
    </div>
</template>
<script>
import mytop from "../small/movetop.vue"
import listone from "../small/listone.vue"
import loading from "../small/loading.vue"
import ColorThief from "../../node_modules/colorthief/dist/color-thief.mjs";

export default {
    name:"album",
    data(){
        return {
            songlistxs:false,
            zjid:0,

            next:0,
            tim:null,
            timer:null,
            timxs:null,
            songlistend:false,

            // 顶部的歌单名
            allname:'新歌速递',
            alb:10,

            // 歌单详情
            imgs:{
                bgimg:'',
                thename:'',
                thexq:'',
                thepeople:''
            },
            // 当前显示的歌曲列表
            songarr:[],
            // 总歌曲列表
            allnum:[],

            bacolor:'',
        }
    },
    components:{
        loading,
        mytop,
        listone
    },
    created(){
        // console.log(this.$route.query)
        this.zjid = this.$route.query.id
        this.getdata()
        // 创建组件的时候回到最开始的地方
        document.documentElement.scrollTop = 0
        document.body.scrollTop = 0
        // 添加滚动事件
        window.addEventListener('scroll', this.listscroll)
        document.body.addEventListener('scroll', this.listscroll)
    },
    methods:{
        getdata(){
            axios.get('https://apis.netstart.cn/music/top/song?type=7').then(
                res=>{
                    // console.log(res)
                    this.imgs.bgimg = res.data.data[0].album.picUrl
                    let newres = res.data.data.slice((this.next*20),(this.next+1)*20)
                    for(let i = 0;i<newres.length;i++){
                        let obj = {}
                        obj.id = newres[i].id
                        obj.name = newres[i].name
                        obj.people = ''
                        for(let j = 0 ;j<newres[i].artists.length;j++){
                            if(j == 0){
                                obj.people = newres[i].artists[j].name
                            }else{
                                obj.people += "/"+newres[i].artists[j].name
                            }
                        }
                        this.songarr.push(obj)
                    }
                    if(this.next == 4){
                        this.songlistend = true
                    }
                },
                err=>{
                    console.log(err)
                }
            )
        },
        listscroll(){
            if(this.$refs.ul !== undefined){
                let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
                //变量windowHeight是可视区的高度
                let windowHeight = document.documentElement.clientHeight || documentbody.clientHeight;
                if(scrollTop >= 200){
                    if(scrollTop+windowHeight >= 260+this.$refs.ul.scrollHeight-1){
                        clearTimeout(this.timer)
                        this.timer = setTimeout(()=>{
                            this.getname()
                        },1000)
                    }
                }
            }
        },
        getname(){
            if(this.songlistend == false){
                this.next += 1
                // console.log(this.next)
                if(this.next <= 4){
                    this.getdata()
                }
            }
        },
        loadImage(){//图片加载完成后调用
            let img = document.querySelector('#myimg');
            img.setAttribute('crossOrigin', 'anonymous')
            if (img.complete) {
                let colorthief = new ColorThief();
		        this.bacolor = colorthief.getPalette(img)[0]; 
                this.songlistxs = true
		        // console.log(this.bacolor);
            } else {
                img.addEventListener('load', function () {
                    let colorthief = new ColorThief();
                    this.bacolor = colorthief.getPalette(img)[0]; 
                    this.songlistxs = true
                    // console.log(this.bacolor); 
                });
            }
		},
    }
}
</script>
<style lang="less" scoped>
.top{
    height: 100%;
    width:100%;
}
.songlistbox{
    position: relative;
    height:100%;
    // height:2200px;
}
.listtoubox{
    // z-index: 1;
    width:100%;
    height:300px;
    overflow: hidden;
    position: relative;
    .listbgimg{
        width: 100%;
        position: relative;
        img{
            width: 100%;
        }
    }
    .zhezhao{
        width: 100%;
        height:300px;
        position: absolute;
        top: 0px;
        left:0px;
        background-color: rgba(0, 0, 0, 0.17);
    }
    .zzd{
        width: 100%;
        height:300px;
    }
    .listsmallbox{
        position: absolute;
        width: 100%;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        display:flex;
        .listleft{
            width: 50%;
            height:100%;
            border-radius: 10px;
            margin: 20px;
            // border:1px solid white;
            font-size: 0px;
            position: relative;
            // .ltitle{
                img{
                    width: 100%;
                    border-radius: 10px;
                }
            // }
            .zjimg{
                position: absolute;
                // background: red;
                top:-30px;
                left:50%;
                transform: translateX(-50%);
                height:30px;
                width:80px;
                overflow: hidden;
                .zj{
                    width:0px;
                    height:0px;
                    border:40px solid black;
                    border-top-left-radius: 50%;
                    border-top-right-radius: 50%;  
                }
                .otherdw{
                    position: relative;
                    .zjdw{
                        position: absolute;
                        top: -55px;
                        left: 50%;
                        transform: translateX(-10px);
                        height: 30px;
                        width: 80px;
                        overflow: hidden;
                        .zzjdw{
                            width:0px;
                            height:0px;
                            border:10px solid red;
                            border-top-left-radius: 50%;
                            border-top-right-radius: 50%;
                        }
                    }
                }
            }
        }
        .listright{
            // margin-top:20px;
            width: 100%;
            .listtitle{
                color: white;
                // display: flex;
                align-items: center;
                width: 100%;
            }
            .listtouimg{
                .zjname{
                    margin-top: 20%;
                    margin-left: 10px;
                    font-size: 25px;
                }
                .zjpeople{
                    margin-top:15px;
                    font-size:13px;
                    margin-bottom: 20px;
                    color:rgba(255, 255, 255, 0.658);
                }
            }
            .xiangqing{
                color: white;
                display: -webkit-box;
                -webkit-line-clamp: 2;
                -webkit-box-orient: vertical;
                overflow: hidden;
                font-size:14px;
                width:80%;
            }
        }

    }
}
.listcontentbox{
    z-index: 100;
    width: 100%;
    // height: 300px;
    background-color: #f8f8f8;
    // background-color: red;
    border-top-left-radius: 40px;
    border-top-right-radius: 40px;
    position: relative;
    padding-top:20px;
    margin-top:-40px;
    min-height:70%;
    padding-bottom: 30px;
}
li{
    padding: 10px;
    box-sizing: border-box;
    margin: 10px auto;
    border-radius: 10px;
    background-color: white;
    width: 90%;
    position: relative;
}
.loading{
    background-color: transparent;
    margin-top: 20px;
}
.zjxqbox{
    margin-top: 80px;
    padding-bottom:20px;
    &::-webkit-scrollbar { width: 0 !important }
    .xqtop{
            width: 40%;
            height:100%;
            border-radius: 10px;
            margin: 20px auto;
            // margin: 20px;
            // border:1px solid white;
            font-size: 0px;
            position: relative;
            .ltitle{
                position: relative;
                z-index: 50;
                img{
                    width: 100%;
                    border-radius: 10px;
                }
            }
            .zjimg{
                z-index: 10;
                position: absolute;
                // background: red;
                top:-30px;
                left:50%;
                transform: translateX(-50%);
                height:30px;
                width:120px;
                overflow: hidden;
                .zj{
                    width:0px;
                    height:0px;
                    border:60px solid black;
                    border-top-left-radius: 50%;
                    border-top-right-radius: 50%;  
                }
            }
        }
    .xqxian{
        width:80%;
        height:1px;
        margin:25px auto;
        background: radial-gradient( circle 120px at center center, rgba(138, 138, 138, 0.678),rgba(0, 0, 0, 0));
    }
    .xqming{
        color:white;
        text-align: center;
        font-size:16px;
    }
    .myxq{
        color:rgba(255, 255, 255, 0.8);
        width:80%;
        margin:0 auto;
        line-height:30px;
    }
}
</style>